home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Ken's Cursing 1.0.1 / cursing.c next >
Encoding:
C/C++ Source or Header  |  1995-12-01  |  5.1 KB  |  192 lines  |  [TEXT/CWIE]

  1. // Ken's Cursing
  2. // version 1.0.1
  3. // by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 and THINK 7 on 951201
  5.  
  6. //• --------------------------------------------------------------- •//
  7. //• An itty bitty bytes™ novice Mac C programmer demo of            •//
  8. //• SPINNING A COLOR CURSOR - without a VBL task.                    •//
  9. //•                                                                 •//
  10. //• Kenneth A. Long                                                    •//
  11. //• aka "itty bitty bytes™"                                            •//
  12. //• kenlong@netcom.com                                                •//
  13. //• 18 November 1995                                                •//
  14. //•                                                                 •//
  15. //• This is the simplest way to spin a color cursor.                •//
  16. //• --------------------------------------------------------------- •//
  17.  
  18. #define over qd.screenBits.bounds.right
  19. #define down qd.screenBits.bounds.bottom
  20.  
  21. CCrsrHandle apple[20];        //• 20 Macintosh apples.
  22. Boolean hues;
  23. Rect windRect;
  24. WindowPtr ibbWind;
  25.  
  26. //• Prototypes:
  27. void SetMyCursor (CCrsrHandle cCrsr);
  28. CCrsrHandle GetMyCursor (short cursID);
  29. void InitSpinner (void);
  30. void Rotate (void);
  31. void InitMacintosh (void);
  32. void main (void);
  33.  
  34. //• Routines:
  35.  
  36. #define rotateTicks 8L            /* Minimum time, in ticks (1/60 sec.)  */
  37. short crsr;                    /* current cursor index                */
  38. long lasttime;                /* tickcount from previous call        */
  39.  
  40. //• --------------------------------------------------------------- •//
  41. //• Sniped from the "SpinCursor" Pascal source.
  42. //• If the Mac has color, it sets the color cursors.  If not, it 
  43. //• does NOT set 'CURS' resources - only the B/W of the 'crsr' res's.
  44. //• Try setting "hues" to false, in InitMac ().
  45.  
  46. void SetMyCursor(CCrsrHandle cCrsr)
  47. {
  48.     if (hues)
  49.         SetCCursor(cCrsr);
  50.     else
  51.         SetCursor((CursPtr)(&cCrsr[0]->crsr1Data)[0]);
  52. }
  53.  
  54. //• --------------------------------------------------------------- •//
  55. //• This *GETS* the resources as prescribed by the "InitSpinner"
  56. //• routine.  The ID number is passed here, and the color res. is
  57. //• passed back IF the Mac has color.  If not, a handle to the 
  58. //• resource is gotten.
  59.  
  60. CCrsrHandle GetMyCursor(short cursID)
  61. {
  62.     Handle h;
  63.  
  64.     if (hues)
  65.     {
  66.         //• GetCIcon doesn't release the resource, so let's do that!
  67.         h = GetResource('crsr', cursID);
  68.         ReleaseResource(h);
  69.         return (GetCCursor(cursID));
  70.     }
  71.     else
  72.         return ((CCrsrHandle) GetResource('crsr', cursID));
  73. }
  74.  
  75. //• --------------------------------------------------------------- •//
  76. //• This tells GetMyCursor which resources to get.  We have 20, in 
  77. //• this case.
  78. //• You put the *quantity* of 'crsr' resources you want to
  79. //• display into the center section of the 'for' loop.
  80. //• If you have a different batch of cursors, for a different action,
  81. //• then use two 'for' loops here - one for each batch.  Then use an
  82. //• 'if' statement to chosse them and call "InitSpinner" again.  The
  83. //• resources are released by GetMyCursor, so it should not eat RAM.
  84.  
  85. void  InitSpinner ()
  86. {
  87.     short i;
  88.     crsr = 0;
  89.     lasttime = 0L;
  90.  
  91.     for (i = 0; i < 20; i++)
  92.         apple[i] = GetMyCursor(128 + i);    //• 'i' is 0 on first pass.
  93. }
  94.  
  95.  
  96. // void  InitSpinner ()        //• Alternate InitSpinner.
  97. // {
  98. //     short i;
  99. //     crsr = 0;
  100. //     lasttime = 0L;
  101. // 
  102. //     if (progress)                                //• A boolean...
  103. //         for (i = 0; i < 20; i++)                //• First has 20.
  104. //             apple[i] = GetMyCursor(128 + i);    //• First set of ID's.
  105. //     
  106. //     if (idling)                                    //• Same here...
  107. //         for (i = 0; i < 20; i++)
  108. //             apple[i] = GetMyCursor(148 + i);    //• Another set.
  109. // }
  110.  
  111.  
  112. //• --------------------------------------------------------------- •//
  113. //• Puts up the Rotating Apple, and rotates it if previously
  114. //• present, and enough time has elapsed since the last rotation.
  115. //• It's called, in this case, in each pass through the main event
  116. //• loop.  It's called, rotates to the next ID, unless the next one
  117. //• is out of range, in which case it reverts back to the first one.
  118. //• But you will want to call it from a 'do/while' loop
  119.  
  120. void  Rotate()
  121. {
  122.     long time;
  123.  
  124.     time = TickCount();
  125.  
  126.     if (time < lasttime)
  127.         return;
  128.     else 
  129.     {
  130.         lasttime = time + rotateTicks;
  131.         crsr++;
  132.         
  133.         //• "crsr" MUST total the quantity plus 0.  In this case,
  134.         //• 0 + 19 = 20, or our total.  If you said "if (crsr > 20)"
  135.         //• it would crash because we only have 20 'crsr's.
  136.         if (crsr > 19)
  137.             crsr = 0;
  138.         SetMyCursor(apple[crsr]);
  139.     }
  140. }
  141.  
  142. void InitMacintosh(void)
  143. {
  144.     SysEnvRec world;
  145.  
  146.     MaxApplZone();
  147.     
  148.     InitGraf(&qd.thePort);
  149.     InitFonts();
  150.     FlushEvents(everyEvent, 0);
  151.     InitWindows();
  152.     TEInit();
  153.     InitCursor();
  154.  
  155.     //• Check for color.
  156.     hues = false;
  157.     if ( SysEnvirons(1, &world) == noErr )    //• Comment out this...
  158.         hues = world.hasColorQD;            //• ...and this for B/W.
  159.  
  160.     windRect.top     = down / 2 - 10;
  161.     windRect.left    = over / 2 - 88;
  162.     windRect.bottom = down / 2 + 10;
  163.     windRect.right    = over / 2 + 88;
  164. }
  165.  
  166. void main(void)
  167. {
  168.     long ticks;
  169.     
  170.     InitMacintosh();
  171.     InitSpinner ();
  172.  
  173.     //• Humongus main event loop!
  174.     while (! Button ())
  175.         Rotate ();
  176.     
  177.     
  178.     //• Ooops!  You clicked!
  179.     InitCursor ();
  180.     SetPort (ibbWind = NewWindow(0L, &windRect, "\p", true, 
  181.                                  plainDBox, (WindowPtr) -1L, true, 0));
  182.     MoveTo (4, 14);
  183.     DrawString ("\pAn itty bitty bytes™ demo!");
  184.     while (! Button ())
  185.         ;        //• Do nothing.
  186. }
  187.  
  188. //• --------------------------------------------------------------- •//
  189. //• NOTE: You could use GetMouse and have call Rotate if the mouse
  190. //• is in your window and InitCursor if it is not.  Or, in various
  191. //• parts of the window, or whatever.
  192.